/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;

import de.renier.jkeepass.JKeePass;
import de.renier.jkeepass.Messages;
import de.renier.jkeepass.dialog.PasswordDialog;
import de.renier.jkeepass.encryption.Cryptor;
import de.renier.jkeepass.util.Utils;


/**
 * OpenAction
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class OpenAction extends AbstractAction {
  
  private static final long serialVersionUID = -2441492422232321206L;
  private File lastDirectory = null;
  private File openFile = null;
  
  public OpenAction() {
    super(Messages.getString("OpenAction.0"),new ImageIcon(OpenAction.class.getResource("/org/javalobby/icons/20x20/Open.gif"))); //$NON-NLS-1$ //$NON-NLS-2$
  }

  public OpenAction(File file) {
    super(file.getAbsolutePath());
    this.openFile = file;
  }
  
  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    // check if data changed
    if (JKeePass.application.isModified()) {
      // dialogbox to warn unsaved data
      int result = JOptionPane.showConfirmDialog(JKeePass.application,Messages.getString("OpenAction.2"), Messages.getString("OpenAction.3") , JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
      if (result == JOptionPane.NO_OPTION) {
        return;
      }
    }
    
    // check if we have a direkt file open
    File file = null;
    if (this.openFile != null) {
      file = this.openFile;
    } else {
      // open file Chooser
      final JFileChooser fc = new JFileChooser();
      if (lastDirectory != null) {
        fc.setCurrentDirectory(lastDirectory);
      }
      fc.setFileFilter(Utils.getPassFileFilter());
      int ret = fc.showOpenDialog(JKeePass.application);
      if (ret == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();
      }
    }
    // if file is selected
    if (file != null) {
      // save last dir
      lastDirectory = file.getParentFile();
      // ask for password
      PasswordDialog passDialog = new PasswordDialog(JKeePass.application, file.getName());
      passDialog.show();
      // check result
      if (passDialog.getResult() == PasswordDialog.RESULT_OK) {
        // open the file
        try {
          // initialize cryptor
          Cryptor.initialize(passDialog.getPassword());
          // open file
          FileInputStream inFile = new FileInputStream(file);
          DefaultMutableTreeNode rootNode = Utils.readNode(inFile);
          JKeePass.application.getElementTreePanel().setDefaultTreeModel(rootNode);
          // set last opened file list
          Utils.setLastOpenedFile(file);
          // refresh menu
          JKeePass.application.refreshLastOpenedFiles();
          JKeePass.application.setModified(false);
        } catch (FileNotFoundException fnfe) {
          JOptionPane.showMessageDialog(JKeePass.application,Messages.getString("OpenAction.4") + file.getAbsolutePath() + Messages.getString("OpenAction.5") + fnfe.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
          file = null;
        } catch (Cryptor.CryptionException ce) {
          System.out.println("Error open File [" + file.getAbsolutePath() + "]: " + ce.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
          JOptionPane.showMessageDialog(JKeePass.application,Messages.getString("OpenAction.8") + file.getAbsolutePath() + Messages.getString("OpenAction.9")); //$NON-NLS-1$ //$NON-NLS-2$
          file = null;
        }
      } else {
        file = null;
      }
    }
    JKeePass.application.setOpenFile(file);
  }

}
